// pages/gift/gift.js
const app = getApp()
var api = require('../../utils/api.js')
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    card_type: 1,
    limit: 20,
    page: 1,
    total_page: 0,
    cardsList: [],
    show_type: 1, //1:送出的 2:收到的
    hasLoadData:false, //是否已经加载数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      hasLoadData: false
    })
    this.cards();
  },

  //页面下拉刷新
  onPullDownRefresh: function () {
    var that = this;
    setTimeout(function () {
      that.resetData()
    }, 1000)
  },
  //刷新页面
  resetData: function () {
    var that = this
    that.setData({
      page: 1,
      // hasMore: true
    });
    that.cards();
  },
  //页面上拉触底
  onReachBottom: function (e) {
    this.loadMore();
  },

  //加载更多
  loadMore: function () {
    if (this.data.page >= this.data.total_page) {
      return;
    }
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      page: this.data.page + 1
    });
    this.cards();
  },

  //礼品卡列表
  cards: function () {
    var that = this;
    api.POST({
      params: {
        page: that.data.page,
        limit: that.data.limit,
        type: that.data.card_type
      },
      url: app.globalData.base_url + 'Giftcard/myCard',
      success: function (res) {
        console.log(res)
        wx.hideLoading()
        wx.stopPullDownRefresh()
        var paging = res.data.data.paging;
        var totalPage = paging.total_page;
        var currentPage = paging.page;
        var resultList = res.data.data.list;
        if (currentPage == 1) {
          var itemData = resultList;
          if (itemData.length >= 1) {
            //有数据
          } else {
            //无数据
          }
        } else {
          var itemData = that.data.cardsList.concat(resultList)
        }
        that.setData({
          hasMore: (currentPage >= totalPage) ? false : true,
          total_page: totalPage,
          cardsList: itemData,
          hasLoadData:true
        })
      },
      fail: function (fail) {
        that.setData({
          hasLoadData: true
        })
        wx.hideLoading()
        wx.stopPullDownRefresh()
        util.showError("登录失败，请检查网络");
      }
    })
  },

  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      var userinfo = app.globalData.accountInfo;
      var uid = userinfo.uid;
      // 来自页面内转发按钮
      var info = res.target.dataset.info;
      var name = info.name;
      var linkCode = info.linkCode;
      var img = info.img;
      return {
        title: name,
        path: '/pages/giftShare/giftShare?uid=' + uid + '&show_type=4&linkCode=' + linkCode,
        imageUrl: img,
        success: function (res) {
          // 转发成功,修改状态为 分享
          api.POST({
            params: {
              linkCode: linkCode,
            },
            url: app.globalData.base_url + 'Giftcard/shareSuccess',
            success: function (res) { }
          })
        },
        fail: function (res) {
          // 转发失败
          console.log(res)
        }
      }
    }
  },



  changeType: function (e) {
    var that = this;
    if (that.data.show_type == e.currentTarget.dataset.type) {
      return;
    }
    that.setData({
      hasLoadData:false,
      show_type: e.currentTarget.dataset.type,
      card_type: e.currentTarget.dataset.type,
      page: 1,
      cardsList:[]
    });
    that.cards();
  },

  //跳转到礼品卡详情
  cardInfo: function (e) {
    var info = e.currentTarget.dataset.info;
    var show_type = this.data.show_type;
    wx.navigateTo({
      url: '../giftShare/giftShare?show_type=' + show_type + '&linkCode=' + info.linkCode
    })
  }
})